<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\BasePlaceServices;
use think\facade\App;

/**
 * Class BasePlace
 * @package app\adminapi\controller\v1\shendeng
 */
class BasePlace extends AuthController
{
    public function __construct(App $app,BasePlaceServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 地点列表
     * @return \think\Response
     */
    public function getList(){
        $where = $this->request->getMore([
            ['keyword',''],
            ['status',''],
            ['city_id',''],
            ['place_type',''],
        ]);
        $admin = $this->request->adminInfo();
        if($admin['store_id']) $where['id'] = $admin['store_id'];
        return app('json')->success($this->services->getList($where,true));
    }

    /**
     * 添加/修改
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        $data = $this->request->postMore([
            ['id',''],
            ['name',''],
            ['city_id',''],
            ['title',''],
            ['short_desc',''],
            ['thumb',''],
            ['latlng',''],
            ['status',''],
            ['store_id','0'],
            ['contact_tel',''],
            ['before_min',''],
            ['cancel_min',''],
            ['cancel_rate',''],
            ['over_cancel_rate',''],
            ['start_time',''],
            ['end_time',''],
            ['place_fee',''],
            ['night_take_fee',''],
            ['night_return_fee',''],
            ['service_cost',''],
            ['oil_service_fee',''],
            ['charge_fee',''],
            ['place_type',''],
            ['place_hour',''],
            ['poiaddress',''],
            ['login_name',''],
            ['pwd',''],
            ['user_name',''],
            ['user_phone',''],
            ['is_person',''],
            ['sfz1',''],
            ['sfz2',''],
            ['qualification',''],
            ['sub_mchid','']
        ]);
        if(!$data['city_id']) return app('json')->fail('请选择城市');
        if(!$data['title']) return app('json')->fail('请输入地点名称');
        if(!$data['contact_tel']) return app('json')->fail('请输入联系电话');
        if(!$data['before_min']) return app('json')->fail('请选择最小预约时间');
        if($data['id']==0){
            if(!$data['login_name']) return app('json')->fail('请设置店员登录账号');
            if(!$data['user_name']) return app('json')->fail('请填写账户名称');
            if(!$data['pwd']) return app('json')->fail('请填写密码');
            if(!$data['user_phone']) return app('json')->fail('请填写店员手机号');
        }
        if(!$data['is_person']) return app('json')->fail('请选择个人/公司');
        if(!$data['sfz1']) return app('json')->fail('请上传身份证正面照');
        if(!$data['sfz2']) return app('json')->fail('请上传身份证反面照');
        if($data['is_person']==2){
            if(!$data['qualification']) return app('json')->fail('请上传资质照片');
        }
        if(!$data['start_time'] || !$data['end_time'] ) return app('json')->fail('请设置营业开始和结束时间');
//        if($data['place_type'] == '0'){// 网点必填提示
//            if(!$data['service_cost']) return app('json')->fail('请输入手续费或车辆整备费');
//            if(!$data['place_fee']) return app('json')->fail('请输入跨网点还车每公里费');
//            if(!$data['night_take_fee'] || !$data['night_return_fee']) return app('json')->fail('夜间取车/还车费不能为空');
//            if(!$data['oil_service_fee'] || !$data['charge_fee']) return app('json')->fail('加油/充电服务费不能为空');
//            if(!$data['cancel_min'] || !$data['cancel_rate'] || !$data['over_cancel_rate']) return app('json')->fail('请完整选择取消规则内容');
//        }
        $this->services->add((int)$data['id'],$data);
        return app('json')->success('提交成功');
    }

    /**
     * 获取组合数据
     * @return mixed
     */
    public function label(){
        $label = sys_data('place_label');
        $list = array();
        foreach ($label as $key=>$item){
            $list[$key]['value'] = $item['title'];
            $list[$key]['label'] = $item['id'];
        }
        return app('json')->success($list);
    }

    /**
     * 地点删除
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->delPlace((int)$data['id']));
    }

    /**
     * 显示/隐藏
     * @param string $status
     * @param string $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setShow(string $status = '', string $id = ''){
        if ($status == '' || $id == '') return app('json')->fail('缺少参数');
        return app('json')->success($this->services->setShow((int)$id, (int)$status));
    }

    /**
     * 地点详细
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPlaceInfo(){
        $data = $this->request->getMore([
            ['id',''],
        ]);
        if(!$data['id']) return app('json')->fail('缺少参数');
        return app('json')->success($this->services->getInfo((int)$data['id']));
    }

    /**
     * 获取地点级联数据
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function placeCascader(){
        return app('json')->success($this->services->placeCascader());
    }

    /**
     * 地点下拉列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function selectPlaceList(){
        return app('json')->success($this->services->selectPlaceList(['is_del'=>0]));
    }

    /**
     * 获取门店
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getStoreList(){
        return app('json')->success($this->services->getStoreList2());
    }

    /**
     * 网点列表
     * @return \think\Response
     */
    public function getOutletList(){
        $where = $this->request->getMore([
            ['keyword',''],
            ['status',''],
            ['place_type',1]
        ]);
        $admin = $this->request->adminInfo();
        if($admin['store_id']) $where['store_id'] = $admin['store_id'];
        return app('json')->success($this->services->getOutletList($where,true));
    }

    /**
     * 网点显示-隐藏
     * @param string $status
     * @param string $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function switchOutlet(string $status = '',string $id = ''){
        if ($status == '' || $id == '') return app('json')->fail('缺少参数');
        return app('json')->success($this->services->switchOutlet((int)$id, (int)$status));
    }

    /**
     * 网点添加修改
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function outletAdd(){
        $data = $this->request->postMore([
            ['id', 0],
            ['city_id',''],
            ['place_type',1],
            ['store_id',''],
            ['title',''],
            ['short_desc',''],
            ['contact_tel',''],
            ['start_time',''],
            ['end_time',''],
            ['latlng',''],
            ['poiaddress',''],
            ['status',''],
            ['before_min',''],
            ['cancel_min',''],
            ['cancel_rate',''],
            ['over_cancel_rate',''],
            ['place_fee',''],
            ['night_take_fee',''],
            ['night_return_fee',''],
            ['service_cost',''],
            ['oil_service_fee',''],
            ['charge_fee',''],
        ]);
        return app('json')->success($this->services->outletAdd((int)$data['id'],$data));
    }

    /**
     * 网点删除
     * @return \think\Response
     */
    public function outletDelete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->outletDelete((int)$data['id']));
    }
}
